package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import com.cn.wanxi.util.IOTool;

import java.util.ArrayList;
import java.util.List;

public class CompanyServiceImpl implements ICompanyService {
    private String split = "\n";
    @Override
    public CompanyModel getCompanyModel() {
        String path = "D:\\wanxi\\step-3\\fuzhuang-03\\web\\note";
        String fileName = "company.txt";

        //创建文件
        IOTool.createNote(path, fileName);

        //写入数据
        List<String> data = setCompanyData();
        String result = IOTool.writeData(data);
        System.out.println("---------------"+result+"=success------写入数据成功--------------");
        System.out.println("---------------"+result+"=error------写入数据失败----没有得到文件note（file）----------");

        //读取数据
        String string = IOTool.readData();

        //封装数据
        CompanyModel companyModel = setData(string);
        return companyModel;
    }


    /**
     * 写入数据
     * 通过方法把数据放到一个list
     * @return
     */
    private List<String> setCompanyData() {
        CompanyDao companyDao = new CompanyDao();
        List<String> list = new ArrayList<>();
        list.add(companyDao.advertisement1);
        list.add(companyDao.advertisement2);
        list.add(companyDao.address);
        list.add(companyDao.linePhone);
        list.add(companyDao.email);
        list.add(companyDao.advertisement3);
        list.add(companyDao.advertisement4);
        return list;
    }

    /**
     * 封装数据
     * 根据读取出来的结果封装成companyModel
     * @param string
     * @return
     */
    private CompanyModel setData(String string) {
        String[] split = string.split(this.split);
        System.out.println("---------------要封装的数据-------------------");
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i] + "----" + i);
        }
        CompanyModel companyModel = new CompanyModel();
        try {
            companyModel.setAdvertisement1(split[0]);
            companyModel.setAdvertisement2(split[1]);
            companyModel.setAddress(split[2]);
            companyModel.setLinePhone(split[3]);
            companyModel.setEmail(split[4]);
            companyModel.setAdvertisement3(split[5]);
            companyModel.setAdvertisement4(split[6]);
        } catch (Exception e) {
        }
        return companyModel;
    }

//    public static void main(String[] args) {
//        new CompanyServiceImpl().getCompanyModel();
//    }
}
